<template>
  <div class="app-container">


    <el-table border stripe v-loading="loading" :data="topicUserdetailList" @selection-change="handleSelectionChange">
      <el-table-column label="题目类型" width="100" align="center" prop="topicType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_topic_type" :value="scope.row.topicType"/>
        </template>
      </el-table-column>

      <el-table-column label="题目" align="center" prop="title"/>
      <el-table-column label="选项1" align="center" prop="choose1"/>
      <el-table-column label="选项2" align="center" prop="choose2"/>
      <el-table-column label="选项3" align="center" prop="choose3"/>
      <el-table-column label="选项4" align="center" prop="choose4"/>
      <el-table-column label="答案" width="80" align="center" prop="answer"/>
      <el-table-column label="用户答案" width="100" align="center" prop="userAnswer"/>
      <el-table-column label="结果" width="80" align="center" prop="isRight">
        <template slot-scope="scope">
          <div v-if="scope.row.isRight == 0" style="color: green;font-size: 16px">
            <i class="el-icon-success"></i>
          </div>
          <div v-else style="color: red;font-size: 16px">
            <i class="el-icon-error"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="解析" align="center" prop="parse"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改用户答题详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
      <!--        <el-form-item label="记录的id" prop="recordId">-->
      <!--          <el-input v-model="form.recordId" placeholder="请输入记录的id"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="题目" prop="title">-->
      <!--          <el-input v-model="form.title" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="答案" prop="answer">-->
      <!--          <el-input v-model="form.answer" placeholder="请输入答案"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="用户答案" prop="userAnswer">-->
      <!--          <el-input v-model="form.userAnswer" placeholder="请输入用户答案"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="0对 1错误" prop="isRight">-->
      <!--          <el-input v-model="form.isRight" placeholder="请输入0对 1错误"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="选项1" prop="choose1">-->
      <!--          <el-input v-model="form.choose1" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="选项2" prop="choose2">-->
      <!--          <el-input v-model="form.choose2" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="选项3" prop="choose3">-->
      <!--          <el-input v-model="form.choose3" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="选项4" prop="choose4">-->
      <!--          <el-input v-model="form.choose4" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="解析" prop="parse">-->
      <!--          <el-input v-model="form.parse" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->
      <!--      <div slot="footer" class="dialog-footer">-->
      <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
      <!--        <el-button @click="cancel">取 消</el-button>-->
      <!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import {
  addTopicUserdetail,
  delTopicUserdetail,
  getTopicUserdetail,
  listTopicUserdetail,
  updateTopicUserdetail
} from "@/api/background/topicUserdetail";

export default {
  dicts: ['app_topic_type'],
  name: "TopicUserdetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户答题详情表格数据
      topicUserdetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordId: [
          {required: true, message: "记录的id不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "题目不能为空", trigger: "blur"}
        ],
        answer: [
          {required: true, message: "答案不能为空", trigger: "blur"}
        ],
        userAnswer: [
          {required: true, message: "用户答案不能为空", trigger: "blur"}
        ],
        isRight: [
          {required: true, message: "0对 1错误不能为空", trigger: "blur"}
        ],
        choose1: [
          {required: true, message: "选项1不能为空", trigger: "blur"}
        ],
        choose2: [
          {required: true, message: "选项2不能为空", trigger: "blur"}
        ],
        choose3: [
          {required: true, message: "选项3不能为空", trigger: "blur"}
        ],
        choose4: [
          {required: true, message: "选项4不能为空", trigger: "blur"}
        ],
        topicType: [
          {required: true, message: "题目类型不能为空", trigger: "change"}
        ],
        parse: [
          {required: true, message: "解析不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询用户答题详情列表 */
    getList() {
      this.loading = true;
      listTopicUserdetail(this.queryParams).then(response => {
        this.topicUserdetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordId: null,
        title: null,
        answer: null,
        userAnswer: null,
        isRight: null,
        choose1: null,
        choose2: null,
        choose3: null,
        choose4: null,
        topicType: null,
        parse: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户答题详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTopicUserdetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户答题详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTopicUserdetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTopicUserdetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户答题详情编号为"' + ids + '"的数据项？').then(function () {
        return delTopicUserdetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/topicUserdetail/export', {
        ...this.queryParams
      }, `topicUserdetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
